<template>
  <div class="app-container org">
    <el-card>
      <el-row>
        <el-col :span="24">
          <p class="pinformation">搜索条件</p>
        </el-col>
      </el-row>
      <el-form ref="queryRef" label-width="100" :model="queryParams">
        <el-row>
          <el-col :span="3">
            <el-form-item label="标准物质名称：" prop="bzwzName" label-width="">
              <el-input clearable maxlength="100" placeholder="请输入标准物质名称" v-model="queryParams.bzwzName"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item label="种类：" prop="bzwzType" label-width="50">
              <el-select placeholder="请选择" clearable v-model="queryParams.bzwzType" filterable>
                <el-option v-for="dict in proxy.addListNoLimit(sys_bzwz_type)" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="唯一性编号：" prop="bzwzCode" label-width="">
              <el-input clearable maxlength="100" placeholder="请输入唯一性编号" v-model="queryParams.bzwzCode"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="批次号：" prop="produceCode" label-width="65">
              <el-input clearable maxlength="100" placeholder="请输入批次号" v-model="queryParams.produceCode"
                @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item label="归属类别：" prop="gsType" label-width="80">
              <el-select placeholder="请选择" clearable v-model="queryParams.gsType" @keyup.enter="handleQuery" filterable>
                <el-option v-for="dict in proxy.addListNoLimit(sys_bzwz_gslb)" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="处置日期：" label-width="80">
              <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" :clearable="false"
                :editable="false" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-form-item label="处置人：" prop="bzwzHandleId" label-width="65">
              <el-select v-model="queryParams.bzwzHandleId" placeholder="请选择处置人" clearable>
                <el-option v-for="item in proxy.addListNoLimit(userList, 'userId', 'nickName')" :key="item.userId"
                  :label="item.nickName" :value="item.userId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="状态：" prop="" label-width="65">
              <el-input clearable maxlength="100" placeholder="请输入状态" />
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-row justify="center">
              <el-button type="success" @click="handleQuery">搜索</el-button>
              <el-button class="resetBtn" @click="resetQuery">重置</el-button>
            </el-row>
          </el-col>
        </el-row>
      </el-form>
      <el-row>
        <p class="pinformation">标准物质处置列表</p>
        <p class="totalStyle">共 {{ total }} 条数据</p>
        <el-table ref="mainTableRef" :data="bzwzTable" style="margin-top: 5px" @selection-change="handleSelChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="唯一性编号" prop="bzwzCode" align="center" />
          <el-table-column label="标准物质名称" prop="bzwzName" align="center" width="230" />
          <el-table-column label="种类" prop="bzwzType" align="center">
            <template #default="scope">
              <dict-tag :options="sys_bzwz_type" :value="scope.row.bzwzType" />
            </template>
          </el-table-column>
          <el-table-column label="生产厂商" prop="produceName" align="center" />
          <el-table-column label="批次号" prop="produceCode" align="center" />
          <el-table-column label="入库日期" prop="createDate" align="center" />
          <el-table-column label="有效日期" prop="yxDate" align="center" />
          <el-table-column label="规格" prop="totalGg" align="center">
            <template #default="scope">
              {{ scope.row.totalGg }}
              <span v-html="transformation(scope.row.ggUnitName)
                "></span>
            </template>
          </el-table-column>
          <el-table-column label="余量" prop="remainGg" align="center" />
          <el-table-column label="处置方式" prop="handleType" align="center">
            <template #default="scope">
              <dict-tag :options="sys_bzwz_handle_type" :value="scope.row.handleType" />
            </template>
          </el-table-column>
          <el-table-column label="处置日期" prop="handleDate" align="center" />
          <el-table-column label="处置人" prop="bzwzHandleId" align="center">
            <template #default="scope">
              {{
                proxy.getItemByArray(
                  userList,
                  "userId",
                  scope.row.bzwzHandleId,
                  "nickName"
                )
              }}
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-row>
    </el-card>
    <!-- 点击行内检测项目数的弹窗 -->
    <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
      v-model="jcxmOpen" @close="closedDialog" :title="title">
      <el-row>
        <p class="pinformation">检测项目列表</p>
        <p class="totalStyle">共 {{ multiplexTable.length }} 条数据</p>
      </el-row>
      <el-table :data="multiplexTable">
        <el-table-column label="检测项目" prop="jcxmName" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.jcxmName)"></span>
          </template>
        </el-table-column>
        <el-table-column label="浓度值" prop="con" align="center" />
        <el-table-column label="浓度值单位" prop="conUnit" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.conUnitName)"></span>
          </template>
        </el-table-column>
        <el-table-column label="不确定度值" prop="notvalue" align="center" />
        <el-table-column label="不确定度单位" prop="notvalueUnit" align="center">
          <template #default="scope">
            <span v-html="transformation(scope.row.subNotvalueName)"></span>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="danger" @click="jcxmOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BzwzDisposal">
import { listHandle } from "@/api/resource/ledger";
import { getUnitList } from "@/api/store/unit";
import { listUser } from "@/api/contract/check";

const { proxy } = getCurrentInstance();
const {
  sys_bzwz_type,
  sys_bzwz_gslb,
  sys_bzwz_gqlx,
  sys_bzwz_jcxm_type,
  sys_bzwz_handle_type,
} = proxy.useDict(
  "sys_bzwz_type",
  "sys_bzwz_gslb",
  "sys_bzwz_gqlx",
  "sys_bzwz_jcxm_type",
  "sys_bzwz_handle_type"
);

const dateRange = ref([]);
const bzwzTable = ref([]);
const multiplexTable = ref([]);
const title = ref("查看检测项目");
const jcxmOpen = ref(false);
const data = reactive({
  queryParams: {
    pageNum: 1, // 当前页数
    pageSize: 10, // 当前页数据条数
    bzwzType: -1,
    gsType: -1,
    bzwzHandleId: -1
  },
});

const { queryParams } = toRefs(data);

const heightParams = ref([{ tableName: "main", bottomHeight: 21 }]);

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}
onMounted(() => {
  nextTick(() => {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  })
})
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

const total = ref(0);
function getList() {
  let temp = {
    "params[beginTime2]": dateRange.value[0],
    "params[endTime2]": dateRange.value[1]
  }
  temp = Object.assign(temp, queryParams.value)
  listHandle(temp).then(
    (res) => {
      bzwzTable.value = res.rows;
      total.value = res.total;
    }
  );
}
getList();

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

function closedDialog() {
  multiplexTable.value = [];
}

const unitList = ref([]);
getUnitList({ unitGroup: 0 }).then((res) => {
  unitList.value = res.data;
});

function handleSelChange(selection) {
  multiplexTable.value = selection;
}

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

function transformation(row) {
  const a = proxy.getSupAndSub(row);
  return a;
}

const userList = ref([]);
listUser().then((res) => {
  userList.value = res.data;
});
</script>

<style lang="scss" scoped>
.el-radio {
  margin-right: 5px;
}
</style>
